package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.ProductionDtoIn;
import com.zyw.zhuangdian.pojo.out.ProductionDtoOut;
import com.zyw.zhuangdian.service.ProductionService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/production")
@RequiredArgsConstructor
@Slf4j
public class ProductionController {
    private final ProductionService productionService;

    /**
     * 新增产品
     * @param json
     * @return
     */
    @RequestMapping(value = "addProduction",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addProduction(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"title","产品名称不能为空");
        Assert.hasKeyAndValue(json,"brand","品牌不能为空");
        Assert.hasKeyAndValue(json,"type","产品类型不能为空");
        Assert.hasKeyAndValue(json,"size","产品规格不能为空");

        ProductionDtoIn in = BeanConvertUtil.covertBean(json, ProductionDtoIn.class);
        Resp<Void> resp = productionService.addProduction(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改产品
     * @param json
     * @return
     */
    @RequestMapping(value = "updateProduction",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateProduction(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"pid","主键ID不能为空");

        ProductionDtoIn in = BeanConvertUtil.covertBean(json, ProductionDtoIn.class);
        Resp<Void> resp = productionService.updateProduction(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除产品
     * @param json
     * @return
     */
    @RequestMapping(value = "deleteProduction",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteProduction(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"pid","主键ID不能为空");

        ProductionDtoIn in = BeanConvertUtil.covertBean(json, ProductionDtoIn.class);
        Resp<Void> resp = productionService.deleteProduction(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出产品
     * @param json privilege,level,(belongTo)
     * @return
     */
    @RequestMapping(value = "listProduction",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listProduction(@RequestBody JSONObject json){
        ProductionDtoIn in = BeanConvertUtil.covertBean(json, ProductionDtoIn.class);
        Resp<List<ProductionDtoOut>> resp = productionService.listProductions(in);
        return RespUtil.OK(resp);
    }
}
